﻿<%@ Page Title="" Language="C#" MasterPageFile="~/forumMaster.Master" AutoEventWireup="true" CodeBehind="Post.aspx.cs" Inherits="forum.Post" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

 <script type="text/javascript">
     var postToUpdate;
     $(document).ready(function () {


         $(".deletePost").live("click", function () {

             if (confirm("Delete post?")) {

                 var parameters;
                 var postId = $(this).attr("postId");
                 parameters = "{'postId':'" + postId + "'}";
                 $.ajax({
                     type: "POST",
                     url: "../Ajax/forumAjax.asmx/deletepost",
                     contentType: "application/json; charset=utf-8",
                     data: parameters,
                     success: function (result) {
                         if (result.d != "bad") {
                             location.reload();
                             return false;
                         }
                     },
                     error: function () {
                         return false;
                     }
                 });
             }
             return false;
         });



         $("#lnkbtnAddPost").click(function () {
             $("#txtaddPostTitle").val("");
             $("#txtAddPostBody").val("");
             open($("#addPostPopUp"));
             return false;
         });


         $("#addPostPopUp").dialog({
             autoOpen: false,
             title: "Add post",
             modal: true,
             buttons: {
                 "ok": function () {

                     var postId = $("#hdnfldPostId").val();
                     var title = $("#txtaddPostTitle").val();
                     var body = $("#txtAddPostBody").val();
                     if (title != "" && body != "") {
                         var parameters = "{'postId':'" + postId + "','title': '" + title + "','body': '" + body + "'}";
                         $.ajax({
                             type: "POST",
                             url: "../Ajax/forumAjax.asmx/addPostToPost",
                             contentType: "application/json; charset=utf-8",
                             data: parameters,
                             dataType: "json",
                             success: function (result) {

                                 if (result.d != "bad") {

                                     setTimeout(function () { location.reload(); }, 1000);
                                     return false;
                                 }
                                 else {
                                     alert("went wrong!");
                                     return false;
                                 }
                             },
                             error: function () {
                             }
                         });



                     }
                     else {
                         alert("Must input Title or Body");
                         return false;
                     }




                     $(this).dialog("close");
                     return false;
                 },
                 "cancel": function () {
                     $(this).dialog("close");
                 }
             }
         });




         $(".editPost").click(function () {
             $("#txteditPostTitle").val("");
             $("#txteditPostBody").val("");
             var postId = $(this).attr("postId");
             postToUpdate = postId;
             var parameters = "{'postId':'" + postId + "'}";
             $.ajax({
                 type: "POST",
                 url: "../Ajax/forumAjax.asmx/getPostDetails",
                 contentType: "application/json; charset=utf-8",
                 data: parameters,
                 dataType: "json",
                 success: function (result) {

                     if (result.d != "bad") {
                         var array = result.d.split(',');
                         var ajaxTitle = array[0];
                         var ajaxBody = array[1];
                         $("#txteditPostTitle").val(ajaxTitle);
                         $("#txteditPostBody").val(ajaxBody);

                         return false;
                     }
                     else {
                         alert("user name and password are wrong!");
                         return false;
                     }
                 },
                 error: function () {
                 }
             });


             open($("#editPostPopUp"));
             return false;
         });


         $("#editPostPopUp").dialog({
             autoOpen: false,
             title: "Edit post",
             modal: true,
             buttons: {
                 "ok": function () {
                     var postId = postToUpdate;
                     var title = $("#txteditPostTitle").val();
                     var body = $("#txteditPostBody").val();

                     if (title != "" || body != "") {
                         var parameters = "{'postId':'" + postId + "','title': '" + title + "','body': '" + body + "'}";
                         $.ajax({
                             type: "POST",
                             url: "../Ajax/forumAjax.asmx/editPost",
                             contentType: "application/json; charset=utf-8",
                             data: parameters,
                             dataType: "json",
                             success: function (result) {

                                 if (result.d != "bad") {

                                     setTimeout(function () { location.reload(); }, 1000);
                                     return false;
                                 }
                                 else {
                                     alert("user name and password are wrong!");
                                     return false;
                                 }
                             },
                             error: function () {
                             }
                         });



                     }
                     else {
                         alert("Must input Title or Body");
                         return false;
                     }




                     $(this).dialog("close");
                     return false;
                 },
                 "cancel": function () {
                     $(this).dialog("close");
                 }
             }
         });


     });
      </script>

       <style type="text/css">
       

       .editPost
       {
           color:Blue;
       }
       
       .editPost:hover
       {
           text-decoration: underline;
           cursor:pointer;
       }
       
       .deletePost
       {
            color:Blue;
       }
       
        .deletePost:hover
       {
           text-decoration: underline;
           cursor:pointer;
       }
       
       
       .post
       {
           margin-top:15px;
       }
       
       #divAddPost
       {
           margin-bottom:20px;
       }
       
       #spanThreadTitle
       {
           color:Red;
       }
       
       #divSubForumName
       {
           margin-top:10px;
       }
       
        #threaTitle
       {
           margin-top:10px;
       }

        #postTitle
       {
           margin-top:10px;
       }
       
        #spanPostTitle
       {
           color:Red;
       }
       
        #lblSubForumeName
       {
           color:Red;
       }
       
       #divAddPost
       {
            margin-top:10px;
       }

              #txtAddPostBody
       {
           width: 255px; 
           height: 172px;
       }
       
       #txtaddPostTitle
       {
         display: block;
       }
       
       
       #txteditPostBody
       {
           width: 255px; 
           height: 172px;
       }
       
       #txteditPostTitle
       {
         display: block;
       }
       
       
       #hdnfldThreadId
       {
           display:none;
       }
       
       

       
    </style>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
     <div id="divPostTitle">
           <h3 id="headPost">View post posts</h3>
    </div>
    
    <div id="divSubForumName">
        <asp:Label ID="lblSubForumeName" ClientIDMode="Static" runat="server" Text=""></asp:Label>
    </div>

    <div id="threaTitle">
        <span id="spanThreadTitle">Thread is:</span>
    </div>
   
    <div id="thread">
             <div class="top">
                   <asp:Label ID="lblThreadUserName" ClientIDMode="Static" runat="server" Text=""></asp:Label> 
                   <asp:Label ID="lblThreadCreationDate" ClientIDMode="Static"  runat="server" Text=""></asp:Label>  
              </div>
              <div class="content">
                    <asp:Label ID="lblThreadTitle" ClientIDMode="Static"  runat="server" Text=""></asp:Label> 
                    <asp:Label ID="lblThreadContent" ClientIDMode="Static"  runat="server" Text=""></asp:Label> 
              </div>
    </div>

    <div id="postTitle">
        <span id="spanPostTitle">Post is:</span>
    </div>

     <div id="post">
             <div class="top">
                   <asp:Label ID="lblPostUserName" ClientIDMode="Static" runat="server" Text=""></asp:Label> 
                   <asp:Label ID="lblPostCreationDate" ClientIDMode="Static"  runat="server" Text=""></asp:Label>  
              </div>
              <div class="content">
                    <asp:Label ID="lblPostTitile" ClientIDMode="Static"  runat="server" Text=""></asp:Label> 
                    <asp:Label ID="lblPostContent" ClientIDMode="Static"  runat="server" Text=""></asp:Label> 
              </div>
    </div>

    <div id="divAddPost">
       <asp:LinkButton ID="lnkbtnAddPost" ClientIDMode="Static" runat="server">Add Post</asp:LinkButton>
   </div>
   <div id="postListTitle">
        <span id="spanPostListTitle">Post List:</span>
    </div>

     <div class="divPostPosts">
            <asp:ListView ID="lvPosts" runat="server" OnItemDataBound="lvPosts_ItemDataBound">
                <EmptyDataTemplate>
                    <span> Post list is empty</span>
                </EmptyDataTemplate>
                <ItemTemplate>
                    <li>
                        <div class="post">
                            <div class="top">
                                <asp:Label ID="lblUserName" class="userNameClass" runat="server" Text=""></asp:Label> 
                                <asp:Label ID="lblCreationDate" class="creationDateClass" runat="server" Text=""><%# Eval("creationDate")%></asp:Label>  
                            </div>
                            <div class="content">
                               <a class="postLink" href="Post.aspx?PostId=<%# Eval("postId") %>" >  <%# Eval("postTitle")%> </a>
                               <asp:Label ID="lblContent" class="contentClass" runat="server" Text=""><%# Eval("postContent")%></asp:Label> 
                            </div>
                            <asp:HyperLink ID="hyplnkEdit" postId='<%# Eval("postId")%>' class="editPost" runat="server">edit</asp:HyperLink>
                            <asp:HyperLink ID="lbkbtnDelete" class="deletePost" postId='<%# Eval("postId")%>' runat="server">delete</asp:HyperLink>
                        </div>
                    </li>
                </ItemTemplate>
                <LayoutTemplate>
                    <ul>
                        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
                    </ul>
                </LayoutTemplate>
            </asp:ListView>
   </div>

       <div id="addPostPopUp">
            <asp:Label ID="lbladdPostTitle" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Title"></asp:Label>
            <asp:TextBox ID="txtaddPostTitle" ClientIDMode="Static" runat="server" ></asp:TextBox>
            <asp:Label ID="lbladdPostBody" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Body"></asp:Label>
            <textarea id="txtAddPostBody" rows="5" cols="5"></textarea>
   </div>

    <div id="editPostPopUp">
            <asp:Label ID="lbleditPostTitle" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Title"></asp:Label>
            <asp:TextBox ID="txteditPostTitle" ClientIDMode="Static" runat="server" ></asp:TextBox>
            <asp:Label ID="lbleditPostBody" class="popUpTitle" ClientIDMode="Static" runat="server" Text="Body"></asp:Label>
            <textarea id="txteditPostBody" rows="5" cols="5"></textarea>
   </div>

      <asp:HiddenField ID="hdnfldPostId"  ClientIDMode="Static" runat="server" />
</asp:Content>
